package com.zut.auth.application.controller;
import com.alibaba.fastjson.JSON;
import com.google.common.base.Preconditions;
import com.zut.auth.application.convert.AuthPermissionDTOConverter;
import com.zut.auth.application.convert.AuthRoleDTOConverter;
import com.zut.auth.application.dto.AuthPermissionDTO;
import com.zut.auth.application.dto.AuthRoleDTO;
import com.zut.common.entity.Result;
import com.zut.domain.entity.AuthPermissionBO;
import com.zut.domain.entity.AuthRoleBO;
import com.zut.domain.sevice.AuthPermissionDomainService;
import com.zut.domain.sevice.AuthRoleDomainService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/permission/")
public class PermissionController {

    @Resource
    private AuthPermissionDomainService authPermissionDomainService;

    /**
     * 添加权限
     */
    @RequestMapping("add")
    public Result<Boolean> add(@RequestBody AuthPermissionDTO authPermissionDTO) {
        Result result =new Result();
        try {
            if (log.isInfoEnabled()) {
                log.info("PermissionController.add.dto:{}", JSON.toJSONString(authPermissionDTO));
            }
            //参数校验
            Preconditions.checkArgument(!StringUtils.isBlank(authPermissionDTO.getPermissionKey()), "权限唯一标识不能为空");
            Preconditions.checkArgument(!StringUtils.isBlank(authPermissionDTO.getName()), "权限名字不能为空");

            AuthPermissionBO authPermissionBO = AuthPermissionDTOConverter.INSTANCE.convertDTOToBO(authPermissionDTO);
            return result.success(authPermissionDomainService.add(authPermissionBO));
        } catch (Exception e) {
            log.error("PermissionController.register.error:{}", e.getMessage(), e);
            return result.fail("注册用户失败");
        }
    }

    /**
     * 修改权限
     */
    @RequestMapping("update")
    public Result<Boolean> update(@RequestBody AuthPermissionDTO authPermissionDTO) {
        Result result =new Result();
        try {
            if (log.isInfoEnabled()) {
                log.info("PermissionController.update.dto:{}", JSON.toJSONString(authPermissionDTO));
            }
            //参数校验
            Preconditions.checkNotNull(authPermissionDTO.getId(),"权限id不能为空");

            AuthPermissionBO authPermissionBO = AuthPermissionDTOConverter.INSTANCE.convertDTOToBO(authPermissionDTO);
            return result.success(authPermissionDomainService.update(authPermissionBO));
        } catch (Exception e) {
            log.error("PermissionController.update.error:{}", e.getMessage(), e);
            return result.fail("更新权限信息失败");
        }
    }


    /**
     * 删除权限
     */
    @RequestMapping("delete")
    public Result<Boolean> delete(@RequestBody AuthPermissionDTO authPermissionDTO) {
        Result result=new Result();
        try {
            if (log.isInfoEnabled()) {
                log.info("PermissionController.delete.dto:{}", JSON.toJSONString(authPermissionDTO));
            }
            AuthPermissionBO authPermissionBO = AuthPermissionDTOConverter.INSTANCE.convertDTOToBO(authPermissionDTO);
            return result.success(authPermissionDomainService.delete(authPermissionBO));
        } catch (Exception e) {
            log.error("PermissionController.update.error:{}", e.getMessage(), e);
            return result.fail("删除权限信息失败");
        }
    }

    /**
     * 获取权限
     */
    @PostMapping("getPermission")
    public Result<List<String>> get(String userName) {
        Result result =new Result();
        try {
            if (log.isInfoEnabled()) {
                log.info("PermissionController.add.dto:{}", userName);
            }
            //参数校验
            Preconditions.checkNotNull(userName,"用户名不能为空！");
            return result.success(authPermissionDomainService.getPermission(userName));
        } catch (Exception e) {
            log.error("PermissionController.register.error:{}", e.getMessage(), e);
            return result.fail("获取用户失败");
        }
    }



}
